package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.service.ArticleManager;
import com.msmall.community.question.model.Question;
import com.msmall.community.question.service.QuestionManager;

/**
 * 通过ID查找对象的标题；目前主要用在后台推荐管理的提示
 *
 */
public class EntityTitleTag extends TagSupport{

	private static final long serialVersionUID = 7284894164855946166L;
	
	private String type;//topic blog question klg
	
	private Long entity; //实体ID
	
	private int style = 1; //实体ID
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		String value = "";
		try {
			if("topic".equals(type)){
				TopicManager tm = (TopicManager) ApplicationManager.appContext.getBean("topicManager");
				Topic topic = (Topic) tm.get(Topic.class, entity);
				if(style == 1){
					value = topic.getTitle();
				}
			}
			if("question".equalsIgnoreCase(type)){
				QuestionManager manager = (QuestionManager) ApplicationManager.appContext.getBean("questionManager");
				Question q = (Question) manager.get(Question.class, entity);
				if(style == 1){
					value = q.getContent10();
				}
			}
			if("klg".equalsIgnoreCase(type)){
				ArticleManager manager = (ArticleManager) ApplicationManager.appContext.getBean("articleManager");
				Article q = (Article) manager.get(Article.class, entity);
				if(style == 1){
					value = q.getTitle();
				}
			}
		} catch (Exception e1) {
			
		}
		
		try {
			writer.print(value);
		} catch (IOException e) {
		}
		return (SKIP_BODY);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Long getEntity() {
		return entity;
	}

	public void setEntity(Long entity) {
		this.entity = entity;
	}

	public int getStyle() {
		return style;
	}

	public void setStyle(int style) {
		this.style = style;
	}
}
